package com.thinkerwu.xxljob.demo.web;

import com.thinkerwu.xxljob.demo.config.XxlJobConfig;
import com.xxl.job.core.biz.model.ReturnT;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;

/**
 * @author wuss45
 * @date 2018年10月16日-23时23分
 */

@RestController
public class IndexController {

    @Resource
    private XxlJobConfig xxlJobConfig;

    @GetMapping("/createJob")
    public ReturnT<String> createJobByCode() {


        RestTemplate template = new RestTemplate();
        template.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));


        MultiValueMap<String, String> jobInfo = new LinkedMultiValueMap<>();

        jobInfo.add("order", Integer.toString(1));
        jobInfo.add("order", Integer.toString(1));
        jobInfo.add("order", Integer.toString(1));
        jobInfo.add("order", Integer.toString(1));
        jobInfo.add("order", Integer.toString(1));
        jobInfo.add("order", Integer.toString(1));
        jobInfo.add("order", Integer.toString(1));
        jobInfo.add("order", Integer.toString(1));
        jobInfo.add("addressType", Integer.toString(0));

        HttpHeaders headers = new HttpHeaders();
        HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(jobInfo, headers);

        return template.postForObject(xxlJobConfig.getAdminAddresses().concat("/jobinfo/add"), httpEntity, ReturnT.class);

    }

}
